/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: dog_configure_manager.cpp
 *
 * Purpose: configure manager helper class implementation
 *
 * Developer:
 *   wen.gu , 2020-08-20
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#define LOG_TAG "CFGM"
#include "dog_configure_manager.h"

#include <string>
#include <map>
#include <mutex>

#include "dog_log.h"

namespace dog
{

/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/

class ConfigureManager::impl
{
public:
    using ConfigureMap = std::map<std::string, IniConfigure*>;
public:
    ConfigureMap mConfigs;
    std::mutex mLock;
};
/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
ConfigureManager::ConfigureManager()
    :mImpl(new impl)
{
    /** todo, something */
}

//static 
ConfigureManager& ConfigureManager::getIstance()
{
    static ConfigureManager confm;

    return confm;
}

ConfigureManager::~ConfigureManager()
{
    /**todo, something */
}

/**
    *@brief load a ini configure file, and give it a short name .
    *@param in fileName ini configure file name(include full path)
    *@param in confName give current configure a short name
    */
bool ConfigureManager::load(ConstString& fileName, ConstString& confName) /** load ini configure file */
{
    AutoLock al(mImpl->mLock);
    if (!fileName.empty() && !confName.empty())
    {
        impl::ConfigureMap::iterator it = mImpl->mConfigs.find(confName);

        if (it == mImpl->mConfigs.end())
        {
            IniConfigure* iniConf = new IniConfigure;
            if (iniConf->load(fileName))
            {
                mImpl->mConfigs[confName] = iniConf;
                return true;
            }

            delete iniConf;
        }
        else
        {
            LOGE("configure short name: %s, already exist\n", confName.c_str());
        }
    }

    return false;
}

const IniConfigure* ConfigureManager::getConfigure(ConstString& confName) /** give configure instance by short name */
{
    AutoLock al(mImpl->mLock);
    impl::ConfigureMap::iterator it = mImpl->mConfigs.find(confName);

    if (it != mImpl->mConfigs.end())
    {
        return it->second;
    }

    return nullptr;
}

} /** namespace dog */
